package site.addzero.repl

import org.koin.core.annotation.Single
import site.addzero.app.AdvancedRepl
import site.addzero.app.ParamDef
import site.addzero.cli.platform.PlatformTools
import kotlin.reflect.typeOf

/**
 * ShowPortCommand的REPL包装类，实现AdvancedRepl接口
 */
@Single
class ShowPortRepl : AdvancedRepl<ShowPortRepl.ShowPortCommandParams, Unit> {
    override val command: String = "show-port"
    override val description: String = "显示端口占用情况"
    override val paramDefs: List<ParamDef> = listOf(
        ParamDef(
            name = "port",
            type = typeOf<Int>(),
            description = "要查看的端口号"
        )
    )

    data class ShowPortCommandParams(
        val port: Int
    )

    override fun eval(params: ShowPortCommandParams) {
        val port = params.port
        if (port <= 0 || port > 65535) {
            System.err.println("端口号必须在 1-65535 范围内")
            return
        }

        println("正在检查端口 $port 的占用情况...")
        val result = PlatformTools.showPort(port)
        println(result)
    }

//    override fun createParams(values: List<Any?>): ShowPortCommandParams {
//        return ShowPortCommandParams(
//            port = values[0] as Int
//        )
//    }
}
